import { RootState } from '@/types/store'
import { useEffect, useRef } from 'react'
import { useDispatch, useSelector } from 'react-redux'

// 封装一些自定义的hooks
export function useInitState<T extends keyof RootState>(
  action: () => void,
  stateName: T
) {
  const dispatch = useDispatch()
  const ActionRef = useRef(action)
  useEffect(() => {
    dispatch(ActionRef.current())
  }, [dispatch])

  const state = useSelector((state: RootState) => state[stateName])
  return state
}
